import { getKeyRouter } from '@/utils';
import { Col, Row } from 'antd';
import { useState } from 'react';
import { connect } from 'umi';
import { ProcdefManageAPI } from '../common/type';
import { ProcdefManageContext, useProcdefManageReducer } from '../mconfig';
import { procdefTableManage, getProcdefTypeManage } from '../services';
import { ManageRightTableView } from './ManageTableView';
import { ManageTreeTypeView } from './ManageTreeTypeView';
import '../common/viewStyles.less';

const ProcdefManageView: React.FC<ProcdefManageAPI.IProcdefManageView> = ({
  curRes,
}) => {
  const prefixCls = 'activiti-procdef-';

  const [searchRenderID, setSearchRenderID] = useState<string>(
    `${prefixCls}${getKeyRouter()}${getKeyRouter()}`,
  );

  /**
   * 获取右侧表格数据
   */
  const {
    getProcdefTableData,
    procdefData,
    loading,
    pagination,
    searchList,
    // manageTableParams,
    otherParams,
    loadMdicMapList,
  } = procdefTableManage(curRes);

  /**
   * 获取reduce
   */
  const {
    leftSelectKeys,
    leftExpandedKeys,
    leftAutoExpandParent,
    procdefDispatch,
    typeSelectRow,
    dicFlowSelectRow,
  } = useProcdefManageReducer();

  /**
   *  获取左侧类型数据
   */
  const { reloadLeftTree, designTypeTree, designTypeLoading } =
    getProcdefTypeManage(curRes);

  /**
   * 右侧表格刷新数据
   */
  const reloadTableData = () => {
    getProcdefTableData({ ...otherParams });
  };

  return (
    <ProcdefManageContext.Provider
      value={{
        curRes,
        prefixCls,
        reloadLeftTree,
        rightTableProps: {
          dataSource: procdefData?.rows,
          loading,
          getProcdefTableData,
          mapList: loadMdicMapList,
          pagination,
          otherParams,
          searchList,
          reloadTableData,
        },
        leftSelectKeys,
        leftExpandedKeys,
        leftAutoExpandParent,
        procdefDispatch,
        typeSelectRow,
        dicFlowSelectRow,
      }}
    >
      <Row
        gutter={[8, 0]}
        className={prefixCls + 'row'}
        id={searchRenderID}
        style={{
          position: 'relative',
          overflow: 'hidden',
        }}
      >
        <Col span={6} className={prefixCls + 'row-col'}>
          <ManageTreeTypeView
            typeTreeLoading={designTypeLoading}
            typeTreeData={designTypeTree}
          />
        </Col>

        <Col span={18} className={prefixCls + 'row-col'}>
          <ManageRightTableView searchRenderID={searchRenderID} />
        </Col>
      </Row>
    </ProcdefManageContext.Provider>
  );
};

export default connect(({ baseLogin, navTab }: Record<string, any>) => ({
  curRes: baseLogin?.permissions,
  navTab,
}))(ProcdefManageView);
